//index.js
//获取应用实例
const app = getApp()
const Util = require('../../utils/util.js');
Page({
  data: {
    cate_id: '',
    token: '',
    imagesData: [],
    images: [],
    share_img: '',
    user_img: '',
    iNum:0
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (e) {
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    });

    wx.getStorage({
      key: 'token',
      success: function (res) {
        if (res.data) {
          that.setData({
            token: res.data,
            cate_id: e.cate_id
          });
          Util.showLoading();
          that.getPhotoDetail(res.data, e.cate_id);
        }
      }
    });
  },
  getPhotoDetail: function (token, cate_id) {
    var that = this;
    Util.getPhotoOne({
      "token": token,
      "cate_id": cate_id
    }, function (re) {
        console.info(re.data);
        that.setData({
          imagesData: re.data,
          imgNumber:re.data.images.length
        });
        const arr = new Object();
        for (var i = 0; i < re.data.images.length; i++) {
          const num = i;
          wx.getImageInfo({
            src: re.data.images[i],
            success: function (ret) {
              var images = that.data.images;
              images[num] = ret.path;
              var iNum = that.data.iNum;
              that.setData({
                images: images,
                iNum: iNum+1
              });
              if (iNum + 1 == re.data.images.length) {
                wx.hideLoading();
              }
            }
          })
        }
        if (re.data.share_img) {
          wx.getImageInfo({
            src: re.data.share_img,
            success: function (ret) {
              that.setData({
                share_img: ret.path
              });
            }
          })
        }
        that.setData({
          imagesData: re.data
        });
      }, function (err) {
        Util.showMsgDialog("操作失败");
    })
  },
  moreFunc: function () {
    wx.navigateTo({
      url: 'test?id=1'
    })
  },
  shareFirend: function () {
    var that = this;
    Util.showLoading('生成中');
    var imgNumber = that.data.imgNumber;
    var iNum = that.data.iNum;
    var imgData = that.data.imagesData;
    var images = that.data.images;
    const cate_id = that.data.cate_id;
    var ctx = wx.createCanvasContext('mycanvas');
    wx.getSystemInfo({
      success: function (gh) {
        ctx.setFillStyle('#fff');
        var winWidth = 350;
        var winHeight = 568;
        if (imgData.share_img){
          ctx.drawImage(that.data.share_img, 0, 0, winWidth, winHeight);
        }else{
          ctx.drawImage('../../images/back.png', 0, 0, winWidth, winHeight);
        }
        wx.downloadFile({
          url: imgData.user_img, //仅为示例，并非真实的资源
          success(reh) {
            ctx.drawImage(reh.tempFilePath, 20, 18, 47, 47);
            ctx.fillStyle = "#000000";
            ctx.setFontSize(16);
            ctx.setTextAlign('left');
            ctx.fillText(imgData.cate_name,80,35);
            ctx.fillStyle = "#aaaaaa";
            ctx.setFontSize(14);
            ctx.setTextAlign('left');
            ctx.fillText(imgData.nickname+'给你分享一个活动相册',80,58);
            var imgNum = images.length;
            var imgWidth = 100;
            var imgHeight = 100;
            for (var i = 0; i < imgNum; i++) {
              if (imgNum > 2) {
                if (i < 3) {
                  var width = 15 + (100 * i) + (i * 10);
                  var height = 90;
                } else if (i < 6) {
                  var width = 15 + (100 * (i - 3)) + ((i - 3) * 10);
                  var height = 205;
                } else if (i < 9) {
                  var width = 15 + (100 * (i - 6)) + ((i - 6) * 10);
                  var height = 315;
                }
              } else if (imgNum == 1) {
                var width = 15;
                var height = 80;
                var imgWidth = 320;
                var imgHeight = 320;
              } else if (imgNum == 2) {
                if (i < 3) {
                  var width = 15 + (155 * i) + (i * 10);
                  var height = 90;
                  var imgWidth = 155;
                  var imgHeight = 155;
                }
              }
              ctx.drawImage(images[i], width, height, imgWidth, imgHeight);
            }
            if (imgNum == 1) {
              height = 220;
            } else if (imgNum == 2) {
              height = 70;
            }
            wx.getImageInfo({
              src: 'https://wx.himayi.cn/Public/upload/code/' + cate_id+'.png',
              success: function (gq) {
                ctx.drawImage(gq.path,267,472,63,62);
                ctx.draw();
                wx.canvasToTempFilePath({
                  x: 0,
                  y: 0,
                  width: winWidth,
                  height: winHeight,
                  canvasId: 'mycanvas',
                  fileType: 'png',
                  success: function (resl) {
                    console.info(resl);
                    wx.saveImageToPhotosAlbum({
                      filePath: resl.tempFilePath,
                      success(res) {
                        console.log("保存图片：success");
                        wx.showToast({
                          title: '保存成功',
                        });
                      },
                      fail(res) {
                        console.log("保存图片：fail");
                        wx.hideLoading();
                      }
                    })
                  }
                });
              }
            });
          }
        })
      }
    });
  },
  onShareAppMessage: function (res) {
    var that = this;
    var cate_id = that.data.cate_id;
    return {
      title: 'Hi,推荐你用用这个小程序',
      path: '/pages/main/content?cate_id=' + cate_id
    }
  }
})